package com.test.listview;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseAdapterMoreActivity extends Activity {
	/** Called when the activity is first created. */
	private final String TAG = "BaseAdapterMoreActivity";

	private ListView mListView;
	private CustomAdapter mAdapter;
	private Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.base_adapter_more);
		mContext = this;
		findAndSettings();
	}

	/**
	 * 查找和设置
	 */
	private void findAndSettings() {
		mListView = (ListView) findViewById(R.id.listview);
		mAdapter = new CustomAdapter(this);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(mContext, "点击位置：" + position, Toast.LENGTH_SHORT).show();
			}
		});
		mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(mContext, "长按位置：" + position, Toast.LENGTH_SHORT).show();
				return true;
			}
		});
		LayoutInflater inflater = LayoutInflater.from(mContext);
		View headView = inflater.inflate(R.layout.head_layout, null);
		mListView.addHeaderView(headView);
		View footView = inflater.inflate(R.layout.foot_layout, null);
		mListView.addFooterView(footView);

		new DataAsyncTask().execute();
	}

	private class CustomAdapter extends BaseAdapter {

		private Context mContext;
		private LayoutInflater mInflater;
		private List<Map<String, Object>> mData;

		public CustomAdapter(Context context) {
			mContext = context;
			mInflater = LayoutInflater.from(context);
			mData = new ArrayList<Map<String, Object>>();
		}

		@Override
		public int getCount() {
			int count = mData == null ? 0 : mData.size();
//			Log.i(TAG, "getCount()=" + count);
			return count;
		}

		@Override
		public Map<String, Object> getItem(int position) {
			if (mData == null || mData.size() == 0) {
				return null;
			} else {
				return mData.get(position);
			}
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView == null){
				convertView = mInflater.inflate(R.layout.simple_adapter_item, null);
				holder = new ViewHolder();
				holder.imgView = (ImageView)convertView.findViewById(R.id.img);
				holder.titleTextView = (TextView)convertView.findViewById(R.id.title);
				holder.infoTextView = (TextView)convertView.findViewById(R.id.info);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			Log.i(TAG, "getView() position==" + position + ",convertView==" + convertView);
			Map<String, Object> map = getItem(position);
			holder.imgView.setImageResource((Integer)map.get("img"));
			holder.titleTextView.setText((String)map.get("title"));
			holder.infoTextView.setText((String)map.get("info"));
			return convertView;
		}

		public void changeData(List<Map<String, Object>> list){
			mData.clear();
			if (list != null && list.size() > 0){
				mData.addAll(list);
			}
			notifyDataSetChanged();
		}

		class ViewHolder {
			public ImageView imgView;
			public TextView titleTextView;
			public TextView infoTextView;
		}
	}

	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "小宗");
		map.put("info", "电台DJ");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "貂蝉");
		map.put("info", "四大美女");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "奶茶");
		map.put("info", "清纯妹妹");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "大黄");
		map.put("info", "是小狗");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "hello");
		map.put("info", "every thing");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "world");
		map.put("info", "hello world");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "1");
		map.put("info", "1111");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "2");
		map.put("info", "222222222");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "3");
		map.put("info", "3333333333");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "4");
		map.put("info", "444444444");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "5");
		map.put("info", "5555555555");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.portraita);
		map.put("title", "6");
		map.put("info", "6666666666");
		list.add(map);

		return list;
	}

	class DataAsyncTask extends AsyncTask<Void, Void, List<Map<String, Object>>>{

		@Override
		protected List<Map<String, Object>> doInBackground(Void... params) {
			return getData();
		}

		@Override
		protected void onPostExecute(List<Map<String, Object>> maps) {
			mAdapter.changeData(maps);
		}
	}
}